package main

import (
	"fmt"
	"net/http"
	"net/url"
)

func serveHttp(w http.ResponseWriter, r *http.Request) {
	// ？后面的
	rawQuery := r.URL.RawQuery
	value, _ := url.ParseQuery(rawQuery)
	fmt.Fprintf(w, "获取URL中rawQuery值为:%s", rawQuery)
	fmt.Fprintf(w, "获取URL中key为name的值为:%v", value.Get("name"))
	fmt.Fprintf(w, "通过HandleFunc启动一个http服务\n")

	// header
	for key := range r.Header {
		fmt.Fprintf(w, "获取Header key为:%s, 值是:%s\n", key, r.Header[key])
	}
}

// 读取body
func gethttpBody(w http.ResponseWriter, r *http.Request) {
	lenght := r.ContentLength
	body := make([]byte, lenght)
	r.Body.Read(body)
	fmt.Fprintf(w, "读取的body为:%s\n", string(body))
}

type defineServiceMux struct {
}

func (dsm *defineServiceMux)serveHttp(w http.ResponseWriter, r *http.Request) {

	fmt.Fprintln(w, "通过自定义类型的HandleFunc启动一个http服务")
}

// web 编程
func main() { //localhost:8080 访问
	http.HandleFunc("/", serveHttp)

	// 增加一个路由
	http.HandleFunc("/getbody", gethttpBody)
	http.ListenAndServe(":8080", nil)
	fmt.Println("building")
}
